package com.wisdomparking.tools;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class ToolSMS {
	
	static final String uid = "juhezhl@sina.com";
	static final String key = "e5e2f965833612a7b9a0";
	
	
	/**
	 * 中国网建 - 发送短信
	 * 
	 * @param mobileNumber   	手机号吗
	 * @param content			发送内容		 
	 * @return
	 */
	public static boolean sendMobileMsg(String mobileNumber, String content) {
		boolean flag = false;
		
		if(!mobileNumber.equals("")){
			try {
				
//				String uid = "juhezhl@sina.com";
//				String key = "e5e2f965833612a7b9a0";
//				
				HttpClient client = new HttpClient();
				PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");
				post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");// 在头文件中设置转码
				NameValuePair[] data = { 
						new NameValuePair("Uid", uid),
						new NameValuePair("Key", key),
						new NameValuePair("smsMob", mobileNumber),
						new NameValuePair("smsText", "短信验证码: " + content) };
				post.setRequestBody(data);

				client.executeMethod(post);

				String result = new String(post.getResponseBodyAsString().getBytes("utf8"));

				post.releaseConnection();
				if (result.equals("1")) {
					flag = true;
				}
				else{
					System.out.println("##: 验证码发送失败 --> " + mobileNumber + " 【" +content+ "】  return: " + result);
				}
				
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}

		return flag;
	}
	
	
	
	// 获取短信剩余数量
	public static String getSMSCount(){
		String count = "";
		
		try {
			
			count = ToolString.doGet("http://sms.webchinese.cn/web_api/SMS", "Action=SMS_Num&Uid="+uid+"&Key="+key+"", "utf8", true);
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	
	public static void main(String[] args) {
		System.out.println(ToolSMS.sendMobileMsg("18801185080", "123456"));
		// System.out.println(ToolSMS.getSMSCount());
	}
	
}